home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / optivc16 / vdemow1.cpp < prev    next >
C/C++ Source or Header  |  1999-03-06  |  10KB  |  255 lines

  1. /*****************************   VDEMOW1.CPP *****************************
  2. *                                                                        *
  3. *                Demo program for                                        *
  4. *               V e c t o r L i b                                        *
  5. *           for Borland C++ for Windows                                  *
  6. *     with OWL 1.0  (shipped with Borland C++ versions 3.0 and 3.1)      *
  7. *                                                                        *
  8. *       Copyright 1996-1998 by Martin Sander                             *
  9. *                                                                        *
  10. *                                                                        *
  11. *       This sample program is meant to demonstrate how to use           *
  12. *       VectorLib within your Windows programs. It is not intended       *
  13. *       to demonstrate best Windows programming.                         *
  14. *                                                                        *
  15. **************************************************************************/
  16.  
  17. /*  The include-file search path must contain
  18.     \bc\include; \bc\classlib\include; \bc\owl\include; \bc\optivec\include
  19.     The library search path must contain
  20.     \bc\lib; bc\classlib\lib; bc\owl\lib; bc\optivec\lib
  21.     You must build a project with the following entries:
  22.     \bc\optivec\vdemow1.cpp
  23.     \bc\optivec\lib\vcl3w.lib
  24.     \bc\owl\lib\owl.def
  25.     (If your Borland C++ directory has a name other than "bc",
  26.      you must use that other name, of course)
  27.     Additionally, you have to link in the class libraries, ObjectWindows,
  28.     and the standard run-time libraries (use Options/Linker/Libraries)
  29. */
  30.  
  31. #define  WIN30
  32. #include <owl.h>
  33. #include <VFstd.h>
  34. #include <VCFstd.h>
  35. #include <VFmath.h>
  36. #include <VIstd.h>
  37. #include <VCFmath.h>
  38. #include <Vgraph.h>
  39. #include <math.h>
  40. #include <string.h>
  41. #include <float.h>
  42. NEWMATHERR
  43.  
  44. class TVecApp : public TApplication
  45. {
  46. public:
  47.   TVecApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  48.     LPSTR lpCmdLine, int nCmdShow)
  49.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  50.   virtual void InitMainWindow();
  51. };
  52.  
  53. _CLASSDEF(TVecWindow)
  54. class TVecWindow : public TWindow
  55. {
  56. public:
  57.   fVector  X1, X2, Y1, Y2, Y3, Y4, Spc, Freq, Win;
  58.   cfVector CX1, CX2;
  59.   iVector  I1;
  60.   ui       vsize, spcsize;
  61.   int      vview;
  62.   struct time tStart, tStop;
  63.   TVecWindow(PTWindowsObject AParent, LPSTR ATitle);
  64.   ~TVecWindow();
  65.   virtual void Paint(HDC DC, PAINTSTRUCT& PS);
  66.   virtual void WMRButtonDown(RTMessage Msg)
  67.     = [WM_FIRST + WM_RBUTTONDOWN];
  68.   void   StartTime( void );
  69.   double StopTime( void );
  70. };
  71.  
  72. TVecWindow::TVecWindow(PTWindowsObject AParent, LPSTR ATitle)
  73.   : TWindow(AParent, ATitle)
  74. {
  75.   vsize  = 1025;
  76.   spcsize = 128;
  77.   vview  =    0;
  78.   I1 = VI_vector( vsize );
  79.   X1 = VF_vector( vsize );
  80.   X2 = VF_vector( vsize );
  81.   Y1 = VF_vector( vsize );
  82.   Y2 = VF_vector( vsize );
  83.   Y3 = VF_vector( vsize );
  84.   Y4 = VF_vector( vsize );
  85.   Spc= VF_vector( spcsize+1 );
  86.   Freq=VF_vector( spcsize+1 );
  87.   Win= VF_vector( 2*spcsize );
  88.   CX1 = VCF_vector( vsize );
  89.   CX2 = VCF_vector( vsize );
  90.  
  91. }
  92.  
  93. TVecWindow::~TVecWindow()
  94. {
  95.   V_nfree( 12, I1, X1, X2, Y1, Y2, Y3, Y4, CX1, CX2, Spc, Freq, Win );
  96. }
  97.  
  98.  
  99. void TVecWindow::WMRButtonDown(RTMessage /*Msg*/)
  100. {
  101.   InvalidateRect(HWindow, NULL, TRUE);
  102.   switch( vview )
  103.   {
  104.      default: vview=-1;
  105.      case 0:  VF_ramp( X1, vsize, 0, 80*M_PI/vsize );
  106.               VF_ramp( Freq, spcsize+1, 0, (vsize / (80*M_PI)) / spcsize );
  107.                 /* get a time axis from 0 to 80 Pi and a corresponding
  108.                    frequency axis from 0 to the Nyquist frequency */
  109.               VF_sin( Y1, X1, vsize );
  110.                     //  get a simple sine wave
  111.               break;
  112.      case 1:  VF_cmp_gtC( Y2, Y1, vsize, 0.7 );
  113.                     //  transform the sine into an asymmetric square wave
  114.               break;
  115.      case 2:  VF_Hanning( Win, 2*spcsize );
  116.               Spc[spcsize] = VFs_spectrum( Spc, spcsize, Y2, vsize, Win );
  117.                     //  analyse the frequency spectrum of the square wave
  118.               break;
  119.      case 3:  VI_ramp( I1, vsize, 0, 1 );
  120.               VF_ramp( X1, vsize, 0, 4.0 /(vsize-1) );
  121.               VF_sinrpi2( Y1, I1, vsize, (vsize-1)/2);
  122.               VFx_equV( Y1, Y1, vsize, 10.0, 20.0 );
  123.               VF_tanrpi2( Y2, I1, vsize, (vsize-1)/2);
  124.               VF_limit( Y2, Y2, vsize, -40, 40 );
  125.               VF_cosecrpi2( Y3, I1, vsize, (vsize-1)/2);
  126.               VF_limit( Y3, Y3, vsize, -40, 40 );
  127.               VF_subC( Y3, Y3, vsize, 20.0 );
  128.               break;
  129.      case 4:  VCF_ramp( CX1, vsize, fcplx( M_PI, 0 ),
  130.                                     fcplx( 0.04, 0.0001 ));
  131.               VCF_cos( CX2, CX1, vsize );
  132.               VCF_sin( CX1, CX1, vsize );
  133.                 /*  Try also with other complex functions! */
  134.               break;
  135.      case 5:  break;
  136.   }
  137.   vview++;
  138. }
  139.  
  140.  
  141. void TVecWindow::Paint(HDC DC, PAINTSTRUCT&)
  142. {
  143.    char     Explanation[100];
  144.    unsigned i, j;
  145.    float    xt, A=1.2, B=-0.13, C=0.85;
  146.  
  147.    V_initPlot( HWindow, DC );
  148.    switch( vview )
  149.    {
  150.        default: break;
  151.        case 0: TextOut( DC, 10, 10,
  152.                "This is a series of graphs illustrating VectorLib functions.", 60 );
  153.                TextOut( DC, 10, 40,
  154.                "Always press the right mouse button to see the next view!", 56 );
  155.                TextOut( DC, 10, 70,
  156.                "Press [Alt] [F4] to end the demonstration.", 41 );
  157.                break;
  158.        case 1: TextOut( DC, 0, 10, "Portion of a", 12 );
  159.                TextOut( DC, 0, 30, "Sine wave", 9 );
  160.                VF_xyAutoPlot( X1, Y1, vsize/4, PS_SOLID, LIGHTRED );
  161.                break;
  162.        case 2: TextOut( DC, 0, 10, "Asymmetric", 10 );
  163.                TextOut( DC, 0, 30, "square wave", 11 );
  164.                V_drawAxes( 0, vsize/4, -0.3, 1.2 );
  165.                VF_yDataPlot( Y2, vsize/4, PS_SOLID, BLUE );
  166.                break;
  167.        case 3: TextOut( DC, 0,  10, "Frequency spectrum", 18 );
  168.                TextOut( DC, 0,  30, "of the square wave", 18 );
  169.                TextOut( DC, 0, 150, "Don't be sur-", 13 );
  170.                TextOut( DC, 0, 170, "prised; the next", 16 );
  171.                TextOut( DC, 0, 190, "example will show", 17 );
  172.                TextOut( DC, 0, 210, "a bit of error", 14 );
  173.                TextOut( DC, 0, 230, "handling!", 9 );
  174.                VF_xyAutoPlot( Freq, Spc, spcsize+1, PS_SOLID | SY_CROSS, GREEN );
  175.                break;
  176.        case 4: TextOut( DC, 0,  10, "Trigonometric", 13 );
  177.                TextOut( DC,10,  30, "functions:", 10 );
  178.                TextOut( DC, 0,  50, "Red:   sine", 11 );
  179.                TextOut( DC, 0,  70, "Green: tangent", 14 );
  180.                TextOut( DC, 0,  90, "Blue:  cosecant", 15 );
  181.                TextOut( DC, 0, 120, "(sine:", 6 );
  182.                TextOut( DC, 0, 140, "   *10 and +20,", 15 );
  183.                TextOut( DC, 0, 160, " cosecant: -20)", 15 );
  184.                VF_xy2AutoPlot( X1, Y2, vsize, PS_SOLID, LIGHTGREEN,
  185.                                X1, Y3, vsize, PS_SOLID, LIGHTBLUE );
  186.                VF_xyDataPlot( X1, Y1, vsize, PS_SOLID, LIGHTRED );
  187.                break;
  188.        case 5: TextOut( DC, 0,  10, "Playing with functions", 22 );
  189.                TextOut( DC, 0,  30, "in the complex plane:", 21 );
  190.                TextOut( DC, 0,  60, "Red:   complex sine",  19 );
  191.                TextOut( DC, 0,  80, "Green: complex cosine",  21 );
  192.                VCF_2AutoPlot( CX1, vsize, PS_SOLID, LIGHTRED,
  193.                               CX2, vsize, PS_SOLID, LIGHTGREEN );
  194.                break;
  195.        case 6: TextOut( DC, 0, 10, "To end the demo, let's make a crude speed comparison", 52 );
  196.                TextOut( DC, 0, 30, "between compiled code and VectorLib code.", 41 );
  197.                V_nfree( 2, X1, X2 );
  198.                X1 = VF_vector( 4096 );
  199.                VF_ramp( X1, 4096, -10, 0.005 );
  200.                X2 = VF_vector( 4096 );
  201.